<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="设备类型" prop="drTypeId">
        <treeselect v-model="queryParams.drTypeId"
                    :options="drtypeinfoOptions"
                    :normalizer="normalizer"
                    placeholder="请选择设备类型"
                    style="width: 215px"
                    @select="handleNodeClick"
        />
      </el-form-item>
      <el-form-item label="设备" prop="apptypeid">
        <el-select v-model="queryParams.drId" placeholder="请选择设备" id="apptypeid" @change="handleQuery" clearable>
          <el-option
            v-for="equipment in equipmentOptions"
            :key="equipment.drid"
            :label="equipment.drname"
            :value="equipment.drid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="详情" prop="details">
        <el-input
          v-model="queryParams.details"
          placeholder="请输入详情"
          clearable
          @keyup.enter.native="handleQuery"
          id="details"
        />
      </el-form-item>
      <el-form-item label="操作人" prop="operationPerson">
        <el-input
          v-model="queryParams.operationPerson"
          placeholder="请输入操作人"
          clearable
          @keyup.enter.native="handleQuery"
          id="operationPerson"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['monitor:control:add']"
        >新增</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['monitor:control:edit']"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['monitor:control:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['monitor:control:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="controlList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目" align="center" prop="projectName" />
      <el-table-column label="设备类型" align="center" prop="drTypeName" />
      <el-table-column label="设备" align="center" prop="drName" />
      <el-table-column label="详情" align="center" prop="details" />
      <el-table-column label="操作人" align="center" prop="operationPerson" />
      <el-table-column label="操作结果" align="center" prop="operationResult" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['monitor:control:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['monitor:control:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运行记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="详情" prop="details">
          <el-input v-model="form.details" placeholder="请输入详情" />
        </el-form-item>
        <el-form-item label="操作人" prop="operationPerson">
          <el-input v-model="form.operationPerson" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="操作结果" prop="operationResult">
          <el-input v-model="form.operationResult" placeholder="请输入操作结果" />
        </el-form-item>
        <el-form-item label="是否上报云端" prop="reported">
          <el-input v-model="form.reported" placeholder="请输入是否上报云端" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listControl, getControl, delControl, addControl, updateControl,drTypeSelect,listDevice } from "@/api/monitor/control";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Control",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运行记录表格数据
      controlList: [],
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: "",
      // 设备类型管理树选项
      drtypeinfoOptions: [],
      // 设备类型树选项
      equipmentTypeOptions: undefined,
      // 设备树选项
      equipmentOptions: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        date: null,
        drTypeId: null,
        drId: null,
        details: null,
        operationPerson: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDrTypeSelect();
  },
  methods: {
    /** 查询运行记录列表 */
    getList() {
      this.loading = true;
      listControl(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.controlList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 转换设备类型管理数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.drtypeid,
        label: node.drtypename,
        children: node.children
      };
    },
    /** 查询设备类型下拉树结构，下拉列表框展示 */
    getDrTypeSelect() {
      drTypeSelect().then(response => {
        this.drtypeinfoOptions = [];
        const data = { drtypeid: 0, drtypename: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "drtypeid", "parentId");
        this.drtypeinfoOptions.push(data);
      });
    },
    // 选择设备类型事件
    /** 点击设备类型去查询设备列表 */
    handleNodeClick(data) {
      this.queryParams.drTypeId = data.drtypeid;
      this.queryParams.drId = null;
      this.handleQuery();
      this.equipmentOptions = [];
      listDevice(this.queryParams.drTypeId).then(response => {
        this.equipmentOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        date: null,
        drTypeId: null,
        drId: null,
        details: null,
        operationPerson: null,
        operationResult: null,
        reported: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.equipmentOptions = [];
      this.queryParams.drId = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运行记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getControl(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运行记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateControl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addControl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除运行记录编号为"' + ids + '"的数据项？').then(function() {
        return delControl(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('monitor/control/export', {
        ...this.queryParams
      }, `control_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
